dojo.provide("lotex.report.Parser");

dojo.require("lotex.report.Helper");
dojo.require("lotex.report.Expression");

lotex.report.Parser = {
    keywords:   ["sum","avg"],
    expCache:   [],
    sheetTag:   'table',
    __sheet:    '__sheet',
    __header:   '__header',
    __data:     '__data',
    __group:    '__group',
    __page:     '__page',
    __all:      '__all',
    __section:  '__section',

    // fast 300 than parse in test
    parseWithCache: function(str,curRow,curCol,sheet,scope) {
        var _cache = this.expCache;
        var l = _cache.length;
        var exp = null;
        for(var i=0; i<l; i++) {
            if(_cache[i].key == str) {
                exp = _cache[i].exp;
                break;
            }
        }

        if(exp==null) {
            exp = new lotex.report.Expression(str);
            exp.precompile();
            var cache = {"key":str,"exp":exp};
            _cache.push(cache);
        }

        return exp.importVar(curRow,curCol,sheet,scope);
    },

    parse: function(str,curRow,curCol) {
        var ex = new lotex.report.Expression(str);

        ex.fullSyntax(curRow,curCol);
        return ex.toExp();
    },

    p2: function(sheet) {
        var y = sheet.getRowLen();
        var x = sheet.getColLen();

        // enum
        var isData=0;
        var isHeader = 1;
        var isGroup = 2;
        var flag;

        sheet.__header = [];
        sheet.__data = [];
        sheet.__group = [];

        for(var row=0; row<y; row++) {
            var tc = 0;
            var css = sheet.rows[row]._classnames;
            flag=-1;
            if (lotex.array.has(css, this.__data)) {
                flag = isData;
                sheet.__data.push(row);
            }
            if (lotex.array.has(css, this.__header)) {
                flag = isHeader;
                sheet.__header.push(row);
            }
            if (lotex.array.has(css, this.__group)) {
                flag = isGroup;
                sheet.__group.push(sheet.rows.splice(row,1)[0]);
                row--;y--;
            }
            for(var col=0; col<x; col++) {
                var cell = sheet.rows[row].cells[col];
                switch (flag){
                    case isData:
                        cell.isData = true;
                        break;
                    case isHeader:
                        break;
                        cell.value = dojo.string.trim(cell.value);

                        var v = cell.value;
                        if (dojo.string.isBlank(cell.value)) {
                            cell.setExpstr(tc);
                            continue;
                        }
                        if (lotex.report.Helper.isColNO(cell.value)) {
                            cell.setExpstr(lotex.math.parseStr(cell.value));
                        } else {
                            tc ++;
                        }
                        break;
                }
            }
        }
    },

    getConfig: function() {
    }
}